package com.ubing.boot.notesforjava.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import sun.rmi.runtime.Log;

import java.io.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;


/**
 * @ClassName : FileDownloadUtils
 * @Description : FileDownloadUtils
 * @Author : ubing
 * @Date: 2023-02-19 16:00
 */
@Component
public class FileDownloadUtils {
    // URL
    public static final String URL="http://localhost:9099/avatar";
    public static final String DOWNLOAD_PATH="E:\\GitHub\\notesforjava\\src\\main\\resources\\static\\avatar";

    // 下载头像
    public static String downAvatar(MultipartFile file,String name) throws IOException {
        byte[] bytes = file.getBytes();
        InputStream inputStream = new ByteArrayInputStream(bytes);
        FileOutputStream fos = new FileOutputStream(DOWNLOAD_PATH+"\\"+name);
        int b;
        while((b = inputStream.read()) != -1){
            fos.write(b);
        }
        fos.close();
        inputStream.close();
        return URL+"/"+name;
    }
    // 多线程下载头像
    public static String threadDownAvatar(MultipartFile file,String name) throws IOException {

        byte[] bytes = file.getBytes();
        int ThreadCount=5;
        int length=bytes.length;
        int size =length/ThreadCount;
        for (int i = 0; i < ThreadCount; i++) {
            if(i==ThreadCount-1){
                byte[] split = new byte[length-(ThreadCount-1)*size];
                System.arraycopy(bytes,i*size,split,0,length-4*size);
                DownLoadThread downLoadThread = new DownLoadThread(i * size, i, DOWNLOAD_PATH + "\\" + name, split);//创建线程下载数据
                downLoadThread.start();
            }else {
                byte[] split = new byte[size];
                System.arraycopy(bytes,i*size,split,0,size);
                DownLoadThread downLoadThread = new DownLoadThread(i * size, i, DOWNLOAD_PATH + "\\" + name, split);//创建线程下载数据
                downLoadThread.start();
            }
        }
        // 自旋
//        while(!(downLoadThreadListener.getCompleteThread()==ThreadCount)){
////            System.out.println("自旋");
//        }

        return URL+"/"+name;
    }

    @Autowired
    private ThreadPoolExecutor executor;
    // 线程池下载头像
    public String ThreadPoolDownAvatar(MultipartFile file,String name) throws IOException {

        byte[] bytes = file.getBytes();
        int ThreadCount=5;
        int length=bytes.length;
        int size =length/ThreadCount;
        for (int i = 0; i < ThreadCount; i++) {
            if(i==ThreadCount-1){
                byte[] split = new byte[length-(ThreadCount-1)*size];
                System.arraycopy(bytes,i*size,split,0,length-4*size);
                executor.execute(new DownLoadThread(i * size, i, DOWNLOAD_PATH + "\\" + name, split));
            }else {
                byte[] split = new byte[size];
                System.arraycopy(bytes,i*size,split,0,size);
                executor.execute(new DownLoadThread(i * size, i, DOWNLOAD_PATH + "\\" + name, split));
            }
        }
        return URL+"/"+name;
    }
    //      spring 自带线程池
    //    @Autowired
    //    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    //      自带线程池注解使用
    @Async("myAsync")
    void asyncDownLoad(int startIndex, int threadId, String path, byte[] bytes) throws FileNotFoundException {
        RandomAccessFile  fos=new RandomAccessFile(path,"rw");
        try {
            fos=new RandomAccessFile(path,"rw");
            InputStream inputStream = new ByteArrayInputStream(bytes);
            int total = 0;
            int b = 0;
            fos.seek(startIndex);
            while((b=inputStream.read())!=-1){
                fos.write(b);
            }
            total += bytes.length;
            System.out.println("线程" + threadId + "下载了" + total);
            inputStream.close();
            fos.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}
